﻿{include file="public/header" /}
<title>数据库备份</title>
</head>

<body>
	<nav class="breadcrumb"><i class="Hui-iconfont">&#xe67f;</i> 首页 <span class="c-gray en">&gt;</span> 系统管理 <span class="c-gray en">&gt;</span> 数据库备份
		<a class="btn btn-success radius r" style="line-height:1.6em;margin-top:3px" href="javascript:location.replace(location.href);" title="刷新"><i class="Hui-iconfont">&#xe68f;</i></a>
	</nav>
	<div class="page-container">

		<div class="cl pd-5 bg-1 bk-gray mt-20">
			<span class="l">
	  <a href="javascript:;"  id="export" class="btn btn-danger radius"><i class="Hui-iconfont"></i> 立即备份</a>
	  <a href="{:url('optimize')}"  class="btn btn-danger radius"><i class="Hui-iconfont"></i> 优化表</a>
	  <a href="{:url('repair')}"  class="btn btn-danger radius"><i class="Hui-iconfont"></i> 修复表</a>
	  </span>
		</div>
		<form id="export-form" method="post" action="{:url('export')}">

			<div class="mt-20">
				<table class="table table-border table-bordered table-bg table-hover table-sort table-responsive">
					<thead>
						<tr class="text-c">
							<th><input class="i-checks checkbox check-all" checked="chedked" type="checkbox"></th>
							<th>表名</th>
							<th>数据量</th>
							<th>数据大小</th>
							<th>创建时间</th>
							<th>备份状态</th>
							<th>操作</th>
						</tr>
					</thead>
					<tbody>
						{notempty name="data"} {volist name="data" id="vo"}
						<tr class="text-c">
							<td><input class="ids i-checks" checked="chedked" type="checkbox" name="ids[]" value="{$vo.name}"></td>
							<td>{$vo.name}</td>
							<td>【{$vo.rows}】 条记录</td>
							<td>{$vo.data_length|format_bytes}</td>
							<td>{$vo.create_time}</td>
							<td id="info">等待备份...</td>
							<td>
								<a class="btn btn-primary btn-xs btn-outline" href="{:url('optimize',['ids'=>$vo['name']])}">优化表</a>
								<a class="btn btn-danger btn-xs btn-outline" href="{:url('repair',['ids'=>$vo['name']])}">修复表</a>
							</td>
						</tr>
						{/volist} {else /}
						<td colspan="7" class="text-center"> 暂未发现数据库表! </td>
						{/notempty}
					</tbody>
				</table>
			</div>
		</form>

		<div id="pageNav" class="pageNav"></div>
	</div>
	{include file="public/footer" /}
	<script type="text/javascript">
		//全选的实现
		$('.check-all').on('ifChecked', function(event) {
			$('input[name="ids[]"]').iCheck('check');
		});
		$('.check-all').on('ifUnchecked', function(event) {
			$('input[name="ids[]"]').iCheck('uncheck');
		});
		$(function() {

			(function($) {
				var $form = $("#export-form"),
					$export = $("#export"),
					tables, $optimize = $("#optimize"),
					$repair = $("#repair");
				$optimize.add($repair).click(function() {
					$.post($(this).attr('url'), $form.serialize(), function(data) {
						if(data.code) {
							layer.msg(data.msg, {
								icon: 1,
								time: 2000,
								shade: 0.1,
							});
						} else {
							layer.msg(data.msg, {
								icon: 2,
								time: 2000,
								shade: 0.1,
							});
						}
					});
					return false;
				});

				$export.click(function() {
					$export.parent().children().prop('disabled', true);
					$export.html("正在发送备份请求...");
					$.post(
						$form.attr("action"),
						$form.serialize(),
						function(data) {
							if(data.code) {
								tables = data.data.tables;
								$export.html(data.msg + "开始备份，请不要关闭本页面！");
								backup(data.data.tab);
								window.onbeforeunload = function() {
									return "正在备份数据库，请不要关闭！";
								};
							} else {
								layer.msg(data.msg, {
									icon: 2,
									time: 2000,
									shade: 0.1,
								});
								$export.html("立即备份");
								setTimeout(function() {
									$export.parent().children().prop('disabled', false);
								}, 1500);
							}
						});
					return false;
				});

				function backup(tab, status) {
					status && showmsg(tab.id, "开始备份...(0%)");
					$.get($form.attr("action"), tab, function(data) {
						if(data.code) {
							showmsg(tab.id, data.msg);
							if(!$.isPlainObject(data.data.tab)) {
								$export.parent().children().prop('disabled', false);
								$export.html("备份完成，点击重新备份");
								window.onbeforeunload = function() {
									return null;
								};
								return;
							}
							backup(data.data.tab, tab.id != data.data.tab.id);
						} else {
							layer.msg(data.msg, 0);
							$export.html("立即备份");
							setTimeout(function() {
								$export.parent().children().prop('disabled', false);
							}, 1500);
						}
					});
				}

				function showmsg(id, msg) {
					$form.find("input[value=" + tables[id] + "]").closest("tr").find("#info").html(msg);
				}
			})(jQuery);
		});
	</script>